﻿#region Using directives

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

using Weazel.Toad.Application.Actions;

#endregion

namespace Weazel.Toad.Application.Controls
{
  [ToolboxItem(true)]
  public partial class ContainerPanel : UserControl
  {
    private class ConsoleOutputControlData
    {
      public string Name;
      public ConsoleOutputControl Control;
      public ToolStripButton Button;

      public ConsoleOutputControlData(string name)
      {
        this.Name = name;

        Control = new ConsoleOutputControl();
        Control.Dock = System.Windows.Forms.DockStyle.Fill;
        Control.Location = new System.Drawing.Point(0, 0);
        Control.Name = "name";
        Control.Size = new System.Drawing.Size(537, 178);
        Control.TabIndex = 1;
        Control.Visible = false;

        Assembly a = Assembly.GetExecutingAssembly();
        System.IO.Stream stream = a.GetManifestResourceStream("Weazel.Toad.Application.Resources.output.png");
        Image img = Image.FromStream(stream);

        Button = new ToolStripButton(name, img);
      }
    }

    public event EventHandler<EventArgs> CurrentPageChanged;

    private Dictionary<string, ConsoleOutputControlData> consoleOutputControls =
      new Dictionary<string, ConsoleOutputControlData>();

    public string GetCurrentPage()
    {
      if(messageListControl.Visible)
        return "ErrorList";
      else 
      {
        foreach (ConsoleOutputControlData c in consoleOutputControls.Values)
          if (c.Control.Visible) 
            return c.Name;
      }

      throw new Exception();
    }

    public void SetCurrentPage(string name)
    {
      foreach (ConsoleOutputControlData c in consoleOutputControls.Values)
      {
        c.Control.Visible = false;
        c.Button.Checked = false;
      }

      messageListControl.Visible = false;
      errorListButton.Checked = false;

      if (name == "ErrorList")
      {
        messageListControl.Visible = true;
        errorListButton.Checked = true;
      }
      else
      {
        consoleOutputControls[name].Control.Visible = true;
        consoleOutputControls[name].Button.Checked = true;
      }

      if (CurrentPageChanged != null)
        CurrentPageChanged(this, EventArgs.Empty);
    }

    public void CreateConsoleOutputControl(string name)
    {
      if (consoleOutputControls.ContainsKey(name))
        throw new DuplicateNameException();

      // create, setup and add the console output control

      ConsoleOutputControlData data = 
        new ConsoleOutputControlData(name);

      this.contentsPanel.Controls.Add(data.Control);
      consoleOutputControls.Add(name, data);
      this.toolStrip.Items.Add(data.Button);

      data.Button.Click += new EventHandler(Button_Click);
    }

    void Button_Click(object sender, EventArgs e)
    {
      System.Diagnostics.Trace.Assert(sender is ToolStripButton);

      foreach (ConsoleOutputControlData data in consoleOutputControls.Values)
        if (data.Button == ((ToolStripButton)sender))
          SetCurrentPage(data.Name);
    }

    public bool ConsoleOutputControlExists(string name)
    {
      return consoleOutputControls.ContainsKey(name); 
    }

    public ConsoleOutputControl GetConsoleOutputControl(string name)
    {
      return consoleOutputControls[name].Control;
    }

    public void RemoveConsoleOutputControl(string name)
    {
      if (!consoleOutputControls.ContainsKey(name))
        throw new KeyNotFoundException();

      ConsoleOutputControl control = 
        consoleOutputControls[name].Control;

      contentsPanel.Controls.Remove(control);
      consoleOutputControls.Remove(name);

      ToolStripButton button =
        consoleOutputControls[name].Button;

      this.toolStrip.Items.Remove(button);
    }

    public ContainerPanel()
    {
      InitializeComponent();

      // create gezel output 
      CreateConsoleOutputControl("Gezel Simulation Output");
    }

    public void BindActions() 
    {
      ActionBase.Actions[typeof(ShowErrorListAction)].BindToolStripItem(errorListButton);
    }

    protected override void OnLoad(EventArgs e)
    {
      
    }

    private void outputButton_Click(object sender, EventArgs e)
    {
    
    }
  }
}
